﻿using _13_webAPI.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace _13_webAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        [HttpGet]
        public Person GetPerson()
        {
            return new Person(1,"杨中科", 18);
        }
        [HttpPost]
        public string[] SaveChange(SaveNoteRequest req)
        {
            System.IO.File.WriteAllText(req.Title + ".txt", req.Content);
            return new string[] { "ok", req.Title };
        }
        [HttpGet]
        public IActionResult ccj(int id)
        {
            if (id == 1)
            {
                return Ok(99);
            }
            else {
                return NotFound("id错误");
            }
        }
        [HttpGet]
        // 自动转化
        public ActionResult<int> ccj2(int id)
        {
            if (id == 1)
            {
                return 99;
            }
            else {
                return NotFound("id错误");
            }
        }
        [HttpGet("{i1}/{i2}")]
        public int Multi(int i1, int i2)
        {
            return i1 + i2;
        }
        [HttpPut("{id}")]
        // 参数和json混用 
        public string UpdatePerson([FromRoute] int id, Models.Person p)
        {
            return "更新ID=" + id + ">>>" + JsonSerializer.Serialize(p);
        }

    }
}
